package com.gitee.fastmybatis.spring.boot.autoconfigure.multi;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 六如
 */
public class MultiDataSourceContext {
    // KEY: dsAlias
    private static final Map<String, MyBatisComponent> COMPONENT_HASH_MAP = new HashMap<>();
    // key: dataSourceId
    private static final Map<String, MyBatisComponent> DATA_SOURCE_KEY_MAP = new HashMap<>();

    public static void setMyBatisComponent(String dsAlias, MyBatisComponent myBatisComponent) {
        COMPONENT_HASH_MAP.put(dsAlias, myBatisComponent);
        DATA_SOURCE_KEY_MAP.put(myBatisComponent.getDataSource().toString(), myBatisComponent);
    }

    public static MyBatisComponent get(DataSource dataSource) {
        return DATA_SOURCE_KEY_MAP.get(dataSource.toString());
    }

    public static MyBatisComponent get(String dsAlias) {
        return COMPONENT_HASH_MAP.get(dsAlias);
    }

}
